/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cl.osera.snippet

import cl.osera._
import model._

import net.liftweb._
import http._
import SHtml._
import S._

import js._
import JsCmds._

import mapper._
import common._
import util._
import Helpers._

import scala.xml.{NodeSeq , Text}

import java.util.Date

object QueryNotDone extends SessionVar(false)

class TD {
  def add (form : NodeSeq) = {
    val todo = ToDo.create.owner ( User.currentUser )
    // val todo = ToDo.create.desc ( "descrip ..." )

    var date = ""
//    var inicio = ""
    
    def checkAndSave() : Unit =
      todo.validate match {
        case Nil => todo.save; S.notice("Added " + todo.desc )
        case xs => S.error(xs) ; S.mapSnippet("TD.add" , doBind )
      }

    def doBind ( form : NodeSeq ) =
      bind ("todo", form,
        "priority" -> todo.priority.toForm,
        // "dateOf" -> todo.dateOf.toForm,
        "dateOf" -> text(todo.dateOf.toString,
          v => { todo.dateOf(todo.dateOf.myDateFormat.parse(v)) },
          "id" -> "entrydate", "size" -> "8"),
        "inicio" -> text(todo.inicio.toString,
          v => { todo.inicio(todo.myToInt(v)) },
          "size" -> "4"),
        "duration" -> text(todo.duration.toString,
          v => { todo.duration(todo.myToInt(v)) },
          "size" -> "4"),
        "desc" -%> todo.desc.toForm,
        "submit" -> submit ("New" , checkAndSave) )
        
    doBind ( form)
  }

  private def toShow =
    ToDo.findAll( By(ToDo.owner, User.currentUser),
      if (QueryNotDone) By(ToDo.done , false )
        else Ignore[ToDo]
      ,OrderBy (ToDo.dateOf, Ascending )
      ,OrderBy (ToDo.inicio, Ascending )
      ,OrderBy (ToDo.duration, Descending )
      ,OrderBy (ToDo.desc, Ascending )
      ,OrderBy (ToDo.done, Ascending )
      ,OrderBy (ToDo.priority, Descending )
      )

  private def desc( td : ToDo, reDraw: () => JsCmd) =
    swappable(<span>{td.desc}</span>
      , <span> {ajaxText(td.desc, v => { td.desc(v).save; reDraw() } )} </span> )

  private def doList (reDraw: () => JsCmd) (html : NodeSeq) : NodeSeq =
    toShow.flatMap ( td =>
      bind("todo", html
        ,"check" -> ajaxCheckbox(td.done, v => {td.done(v).save; reDraw() } )
        ,"priority" -> ajaxSelect(ToDo.priorityList, Full(td.priority.toString)
          ,v => { td.priority(v.toInt).save; reDraw() })
        ,"dateOf" -> ajaxText(td.dateOf.toString
          ,v => { td.dateOf(td.dateOf.myDateFormat.parse(v)).save; reDraw() }
          , "size" -> "8")
        ,"inicio" -> ajaxText(td.inicio.toString
          ,v => { td.inicio(td.myToInt(v)).save; reDraw() }
          , "size" -> "4")
        ,"duration" -> ajaxText(td.duration.toString
          ,v => { td.duration(td.myToInt(v)).save; reDraw() }
          , "size" -> "4")
        ,"desc" -> desc(td, reDraw)
        ) )


//        ,"dateOf" -> ajaxText(td.dateOf.toString
//          ,v => { td.dateOf(Util.slashDate.parse(v)).save; reDraw() })
// td.dateOf.dateFormat.parse(v)

  def list (html : NodeSeq) = {
    val id = S.attr("all_id").open_!

   def inner() : NodeSeq = {
      def reDraw() = SetHtml( id, inner() )

   bind("todo", html
     ,"exclude" -> ajaxCheckbox( QueryNotDone, v => {QueryNotDone(v); reDraw } )
     ,"list" -> doList(reDraw) _)
   }
   
   inner()
  }
}
